#include "PlainTextNode.h"
#include "..\base\Engine.h"
#include "..\utils\Logger.h"

namespace ds {

PlainTextNode::PlainTextNode(const std::string& name,const std::string& fontResourceName) : Node(name,"DefaultMaterial",true) , m_FontName(fontResourceName) {
}


PlainTextNode::~PlainTextNode(void) {
}

void PlainTextNode::onCreateDevice() {
	IDirect3DDevice9 * pDevice = gEngine->getDevice();
	HR(D3DXCreateSprite( pDevice, &m_Sprite ));
}

void PlainTextNode::onDestroyDevice() {
	for (TextMapIterator tmi = m_TextMap.begin(); tmi != m_TextMap.end(); ++tmi) {	
		delete m_TextMap[(*tmi).first];
	}
	m_TextMap.clear();
	SAFE_RELEASE(m_Sprite);	
}

void PlainTextNode::setText(const std::string& reference,const Vec2& pos,char* format,...) {
	va_list args;
	va_start(args,format);
	setText(reference,pos,Color(1.0f,1.0f,1.0f,1.0f),format,args);
	va_end(args);
}

void PlainTextNode::setText(const std::string& reference,const Vec2& pos,char* format,va_list args) {
	setText(reference,pos,Color(1.0f,1.0f,1.0f,1.0f),format,args);
}

void PlainTextNode::setText(const std::string& reference,const Vec2& pos,const Color& color,char* format,...) {
	va_list args;
	va_start(args,format);
	setText(reference,pos,color,format,args);
	va_end(args);
}

void PlainTextNode::setText(const std::string& reference,const Vec2& pos,const Color& color,char* format,va_list args) {
	if ( m_TextMap.find(reference) == m_TextMap.end() ) {
		TextLine *tl = new TextLine();
		m_TextMap[reference] = tl;
	}
	TextLine *tl = m_TextMap[reference];	
	char buffer[1024];
	memset(buffer, 0, sizeof(buffer));
	int written = vsnprintf_s(buffer,sizeof(buffer),_TRUNCATE,format,args);		
	tl->message = std::string(buffer);
	tl->pos = pos;
	tl->color = color;
}

void PlainTextNode::removeText(const std::string& reference) {
}

void PlainTextNode::hideText(const std::string& reference) {
}

void PlainTextNode::showText(const std::string& reference) {
}

TextLine* PlainTextNode::getText(const std::string& reference) {
	if ( m_TextMap.find(reference) != m_TextMap.end() ) {
		return m_TextMap[reference];
	}
	return 0;
}

void PlainTextNode::calculateSize(const std::string& reference,Vec2& size) {
	TextLine* tl = m_TextMap[reference];
	if ( tl != 0 ) {
		ID3DXFont *font = gEngine->getResourceManager().getFont(m_FontName.c_str());	
		RECT font_rect;	
		SetRect(&font_rect,0,0,0,0);	
		font->DrawTextA(m_Sprite,tl->message.c_str(),-1,&font_rect,DT_LEFT|DT_TOP|DT_NOCLIP|DT_CALCRECT,tl->color);		
		size.x = font_rect.right - font_rect.left;
		size.y = font_rect.bottom - font_rect.top;
	}
}

void PlainTextNode::draw() {
	IDirect3DDevice9 * pDevice = gEngine->getDevice();
	ID3DXFont *font = gEngine->getResourceManager().getFont(m_FontName.c_str());	
	if ( font != 0 ) {
		TextMapIterator tmi;
		RECT font_rect;	
		m_Sprite->Begin( D3DXSPRITE_ALPHABLEND );
		for ( tmi = m_TextMap.begin(); tmi != m_TextMap.end(); ++tmi) {	
			TextLine *tl = (*tmi).second;
			if ( tl->active ) {
				int xp = (int)tl->pos.x;
				int yp = (int)tl->pos.y;
				SetRect(&font_rect,xp,yp,0,0);	
				font->DrawTextA(m_Sprite,tl->message.c_str(),-1,&font_rect,DT_CALCRECT,tl->color);		
				font->DrawTextA(m_Sprite,tl->message.c_str(),-1,&font_rect,DT_LEFT|DT_NOCLIP,tl->color);		
			}
		}
		m_Sprite->End();
	}
}

};
